using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.Linq;
using System.Linq;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Const = GM.Constants;
using System.Collections.Generic;

using System.Web;
using System.Net;
using System.Drawing.Imaging;

using EnCryptDecrypt;

namespace ImageUpload {
    public class FormGM : System.Windows.Forms.Form {
        
        #region Controls

        private System.Windows.Forms.GroupBox gbGFX;
        private System.Windows.Forms.GroupBox gbFTP;
        private System.Windows.Forms.ComboBox cbFTP;
        private System.Windows.Forms.Button bnAddImage;
        private System.Windows.Forms.OpenFileDialog openFileDialog;
        private System.Windows.Forms.ListView lvImages;
        private System.Windows.Forms.Button bnExit;
        private System.Windows.Forms.ImageList imageList;
        private System.Windows.Forms.Button btnRemove;
        private System.Windows.Forms.Button btnUpload;
        private System.ComponentModel.IContainer components;
        private System.Windows.Forms.Button btnNewAccount;
        private System.Windows.Forms.Button btnFtpSettings;
        private System.Windows.Forms.Button btnRemoveFTP;
        private System.Windows.Forms.Button btnTestAccount;
        private System.Windows.Forms.Label lblInfo;
        private System.Windows.Forms.GroupBox groupBox1;
        private Button btnRotate90;
        private Button btnRotate270;
        private Button btnMoveRight;
        private Button btnMoveLeft;

        #endregion

        #region Properties

        public static Database DataContext = new Database(@"Storage.sdf");
        public List<Account> accounts = new List<Account>();
        public static Account SelectedAccount = new Account();
        private List<string> imageNames = new List<string>();

        #endregion

        #region Form

        public FormGM() {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
            openFileDialog.InitialDirectory = Application.ExecutablePath;
            openFileDialog.Multiselect = true;
            openFileDialog.Filter = "image files (*.jpg, *.jpeg, *.gif, *.bmp)|*.jpg;*.gif;*.jpeg;*.bmp";
            RefreshFtpCombo();
        }

        protected override void Dispose(bool disposing) {
            DataContext.Dispose();

            if (disposing) {
                if (components != null) {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }
        
        #endregion

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent() {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormGM));
            this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
            this.gbGFX = new System.Windows.Forms.GroupBox();
            this.bnExit = new System.Windows.Forms.Button();
            this.gbFTP = new System.Windows.Forms.GroupBox();
            this.btnTestAccount = new System.Windows.Forms.Button();
            this.btnRemoveFTP = new System.Windows.Forms.Button();
            this.btnNewAccount = new System.Windows.Forms.Button();
            this.btnUpload = new System.Windows.Forms.Button();
            this.btnFtpSettings = new System.Windows.Forms.Button();
            this.cbFTP = new System.Windows.Forms.ComboBox();
            this.lvImages = new System.Windows.Forms.ListView();
            this.imageList = new System.Windows.Forms.ImageList(this.components);
            this.lblInfo = new System.Windows.Forms.Label();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.btnMoveRight = new System.Windows.Forms.Button();
            this.btnMoveLeft = new System.Windows.Forms.Button();
            this.btnRotate90 = new System.Windows.Forms.Button();
            this.btnRotate270 = new System.Windows.Forms.Button();
            this.btnRemove = new System.Windows.Forms.Button();
            this.bnAddImage = new System.Windows.Forms.Button();
            this.gbGFX.SuspendLayout();
            this.gbFTP.SuspendLayout();
            this.groupBox1.SuspendLayout();
            this.SuspendLayout();
            // 
            // gbGFX
            // 
            this.gbGFX.Controls.Add(this.btnMoveRight);
            this.gbGFX.Controls.Add(this.btnMoveLeft);
            this.gbGFX.Controls.Add(this.btnRotate90);
            this.gbGFX.Controls.Add(this.btnRotate270);
            this.gbGFX.Controls.Add(this.btnRemove);
            this.gbGFX.Controls.Add(this.bnAddImage);
            this.gbGFX.Location = new System.Drawing.Point(8, 8);
            this.gbGFX.Name = "gbGFX";
            this.gbGFX.Size = new System.Drawing.Size(96, 103);
            this.gbGFX.TabIndex = 1;
            this.gbGFX.TabStop = false;
            this.gbGFX.Text = "Pictures";
            // 
            // bnExit
            // 
            this.bnExit.Location = new System.Drawing.Point(6, 74);
            this.bnExit.Name = "bnExit";
            this.bnExit.Size = new System.Drawing.Size(104, 23);
            this.bnExit.TabIndex = 2;
            this.bnExit.Text = "Exit";
            this.bnExit.Click += new System.EventHandler(this.bnExit_Click);
            // 
            // gbFTP
            // 
            this.gbFTP.Controls.Add(this.btnTestAccount);
            this.gbFTP.Controls.Add(this.btnRemoveFTP);
            this.gbFTP.Controls.Add(this.btnNewAccount);
            this.gbFTP.Controls.Add(this.btnUpload);
            this.gbFTP.Controls.Add(this.btnFtpSettings);
            this.gbFTP.Controls.Add(this.cbFTP);
            this.gbFTP.Location = new System.Drawing.Point(112, 8);
            this.gbFTP.Name = "gbFTP";
            this.gbFTP.Size = new System.Drawing.Size(229, 103);
            this.gbFTP.TabIndex = 2;
            this.gbFTP.TabStop = false;
            this.gbFTP.Text = "FTP";
            // 
            // btnTestAccount
            // 
            this.btnTestAccount.Location = new System.Drawing.Point(160, 43);
            this.btnTestAccount.Name = "btnTestAccount";
            this.btnTestAccount.Size = new System.Drawing.Size(56, 23);
            this.btnTestAccount.TabIndex = 6;
            this.btnTestAccount.Text = "Test";
            this.btnTestAccount.Click += new System.EventHandler(this.btnTestAccount_Click);
            // 
            // btnRemoveFTP
            // 
            this.btnRemoveFTP.Location = new System.Drawing.Point(8, 43);
            this.btnRemoveFTP.Name = "btnRemoveFTP";
            this.btnRemoveFTP.Size = new System.Drawing.Size(56, 23);
            this.btnRemoveFTP.TabIndex = 5;
            this.btnRemoveFTP.Text = "Remove";
            this.btnRemoveFTP.Click += new System.EventHandler(this.btnRemoveFTP_Click);
            // 
            // btnNewAccount
            // 
            this.btnNewAccount.Location = new System.Drawing.Point(72, 43);
            this.btnNewAccount.Name = "btnNewAccount";
            this.btnNewAccount.Size = new System.Drawing.Size(80, 23);
            this.btnNewAccount.TabIndex = 4;
            this.btnNewAccount.Text = "New account";
            this.btnNewAccount.Click += new System.EventHandler(this.btnNewAccount_Click);
            // 
            // btnUpload
            // 
            this.btnUpload.Location = new System.Drawing.Point(8, 74);
            this.btnUpload.Name = "btnUpload";
            this.btnUpload.Size = new System.Drawing.Size(208, 23);
            this.btnUpload.TabIndex = 2;
            this.btnUpload.Text = "Resize + Upload";
            this.btnUpload.Click += new System.EventHandler(this.btnUpload_Click);
            // 
            // btnFtpSettings
            // 
            this.btnFtpSettings.Location = new System.Drawing.Point(160, 14);
            this.btnFtpSettings.Name = "btnFtpSettings";
            this.btnFtpSettings.Size = new System.Drawing.Size(54, 23);
            this.btnFtpSettings.TabIndex = 1;
            this.btnFtpSettings.Text = "Settings";
            this.btnFtpSettings.Click += new System.EventHandler(this.bnFtpSettings_Click);
            // 
            // cbFTP
            // 
            this.cbFTP.Location = new System.Drawing.Point(8, 16);
            this.cbFTP.Name = "cbFTP";
            this.cbFTP.Size = new System.Drawing.Size(144, 21);
            this.cbFTP.TabIndex = 0;
            this.cbFTP.SelectedIndexChanged += new System.EventHandler(this.cbFTP_SelectedIndexChanged);
            // 
            // lvImages
            // 
            this.lvImages.LargeImageList = this.imageList;
            this.lvImages.Location = new System.Drawing.Point(8, 117);
            this.lvImages.Name = "lvImages";
            this.lvImages.Size = new System.Drawing.Size(459, 324);
            this.lvImages.TabIndex = 0;
            this.lvImages.UseCompatibleStateImageBehavior = false;
            // 
            // imageList
            // 
            this.imageList.ColorDepth = System.Windows.Forms.ColorDepth.Depth24Bit;
            this.imageList.ImageSize = new System.Drawing.Size(100, 100);
            this.imageList.TransparentColor = System.Drawing.Color.Transparent;
            // 
            // lblInfo
            // 
            this.lblInfo.Location = new System.Drawing.Point(10, 16);
            this.lblInfo.Name = "lblInfo";
            this.lblInfo.Size = new System.Drawing.Size(104, 50);
            this.lblInfo.TabIndex = 3;
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.lblInfo);
            this.groupBox1.Controls.Add(this.bnExit);
            this.groupBox1.Location = new System.Drawing.Point(347, 8);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(120, 103);
            this.groupBox1.TabIndex = 4;
            this.groupBox1.TabStop = false;
            // 
            // btnMoveRight
            // 
            this.btnMoveRight.Image = global::ImageUpload.Properties.Resources.right;
            this.btnMoveRight.Location = new System.Drawing.Point(51, 45);
            this.btnMoveRight.Name = "btnMoveRight";
            this.btnMoveRight.Size = new System.Drawing.Size(37, 23);
            this.btnMoveRight.TabIndex = 5;
            this.btnMoveRight.UseVisualStyleBackColor = true;
            this.btnMoveRight.Click += new System.EventHandler(this.btnMoveRight_Click);
            // 
            // btnMoveLeft
            // 
            this.btnMoveLeft.Image = global::ImageUpload.Properties.Resources.left;
            this.btnMoveLeft.Location = new System.Drawing.Point(8, 45);
            this.btnMoveLeft.Name = "btnMoveLeft";
            this.btnMoveLeft.Size = new System.Drawing.Size(37, 23);
            this.btnMoveLeft.TabIndex = 4;
            this.btnMoveLeft.UseVisualStyleBackColor = true;
            this.btnMoveLeft.Click += new System.EventHandler(this.btnMoveLeft_Click);
            // 
            // btnRotate90
            // 
            this.btnRotate90.Image = global::ImageUpload.Properties.Resources._90;
            this.btnRotate90.Location = new System.Drawing.Point(51, 74);
            this.btnRotate90.Name = "btnRotate90";
            this.btnRotate90.Size = new System.Drawing.Size(37, 23);
            this.btnRotate90.TabIndex = 3;
            this.btnRotate90.UseVisualStyleBackColor = true;
            this.btnRotate90.Click += new System.EventHandler(this.btnRotate90_Click);
            // 
            // btnRotate270
            // 
            this.btnRotate270.Image = global::ImageUpload.Properties.Resources._270;
            this.btnRotate270.Location = new System.Drawing.Point(8, 74);
            this.btnRotate270.Name = "btnRotate270";
            this.btnRotate270.Size = new System.Drawing.Size(37, 23);
            this.btnRotate270.TabIndex = 2;
            this.btnRotate270.UseVisualStyleBackColor = true;
            this.btnRotate270.Click += new System.EventHandler(this.btnRotate270_Click);
            // 
            // btnRemove
            // 
            this.btnRemove.Image = global::ImageUpload.Properties.Resources.del;
            this.btnRemove.Location = new System.Drawing.Point(51, 16);
            this.btnRemove.Name = "btnRemove";
            this.btnRemove.Size = new System.Drawing.Size(37, 23);
            this.btnRemove.TabIndex = 1;
            this.btnRemove.Click += new System.EventHandler(this.btnRemove_Click);
            // 
            // bnAddImage
            // 
            this.bnAddImage.Image = global::ImageUpload.Properties.Resources.add;
            this.bnAddImage.Location = new System.Drawing.Point(8, 16);
            this.bnAddImage.Name = "bnAddImage";
            this.bnAddImage.Size = new System.Drawing.Size(37, 23);
            this.bnAddImage.TabIndex = 0;
            this.bnAddImage.Click += new System.EventHandler(this.bnAddImage_Click);
            // 
            // FormGM
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(475, 453);
            this.Controls.Add(this.gbFTP);
            this.Controls.Add(this.gbGFX);
            this.Controls.Add(this.lvImages);
            this.Controls.Add(this.groupBox1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MaximizeBox = false;
            this.Name = "FormGM";
            this.Text = "Galery Manager";
            this.gbGFX.ResumeLayout(false);
            this.gbFTP.ResumeLayout(false);
            this.groupBox1.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        #endregion

        #region Events

        private void bnAddImage_Click(object sender, System.EventArgs e) {
            if (openFileDialog.ShowDialog() == DialogResult.OK) {
                foreach (string fileName in openFileDialog.FileNames) {
                    imageNames.Add(fileName);
                }
                ReloadImages();
            }
        }

        private void bnExit_Click(object sender, System.EventArgs e) {
            this.Close();
        }

        private void btnRemove_Click(object sender, System.EventArgs e) {
            foreach (ListViewItem selectedItem in lvImages.SelectedItems) {
                imageNames.RemoveAt(selectedItem.Index);
                selectedItem.Remove();
            }
        }

        private void bnFtpSettings_Click(object sender, System.EventArgs e) {
            if (cbFTP.SelectedIndex >= 0) {
                Account account = (Account)cbFTP.SelectedItem;
                FTPSettings ftpSettingsDialog = new FTPSettings(account.Id);
                if (ftpSettingsDialog.ShowDialog() == DialogResult.OK) {

                }
                RefreshFtpCombo();
            }
            else {
                FTPSettings ftpSettingsDialog = new FTPSettings();
                if (ftpSettingsDialog.ShowDialog() == DialogResult.OK) {

                }
                RefreshFtpCombo();
            }
        }

        private void btnUpload_Click(object sender, System.EventArgs e) {
            if (imageNames.Count == 0) {
                MessageBox.Show("Nothing to Upload!");
                return;
            }

            FTPProgress progress = new FTPProgress();
            progress.Show();

            Account account = (Account)cbFTP.SelectedItem;

            FtpClient ftpClient = new FtpClient(account.Url, account.Username, CryptorEngine.Decrypt(account.Password, true, account.Username));

            progress.Status++;
            progress.RefreshStatus();
            int fileNumber = 0;
            string[] files = new string[] { };
            for (int i = 0; i < 5; i++) {
                try {
                    files = ftpClient.GetFileList(account.ImageFolder, account.ImagePrefix + "*.jpg");
                    break;
                }
                catch (Exception ex) {
                    MessageBox.Show(ex.Message);
                    if (i > 3) {
                        progress.Close();
                        MessageBox.Show("File list not reached, try again");
                        return;
                    }
                }
            }

            foreach (string fileName in files) {
                if (fileName.Length > 3) {
                    try {
                        int i = Int32.Parse(fileName.Replace(account.ThumbPrefix, "").ToLower().Replace(".jpg", ""));
                        if (i >= fileNumber) fileNumber = (i + 1);
                    }
                    catch { }
                }
            }


            progress.Status++;
            progress.RefreshStatus();

            int uploadedImagesCount = 0;
            string tempFolder = Guid.NewGuid().ToString();

            foreach (string fileName in imageNames) {
                ThumbMaker thumbMaker = new ThumbMaker(fileName);
                FileInfo fi = new FileInfo(fileName);
                string fDir = fi.DirectoryName;
                string fName = fi.Name.ToLower();
                string fExt = fi.Extension.ToLower();
                string ftpName = fileNumber.ToString("00000") + ".jpg";
                string bigName = fDir + "\\" + tempFolder + "\\" + account.ImagePrefix + ftpName;
                string thumbName = fDir + "\\" + tempFolder + "\\" + account.ThumbPrefix + ftpName;
                Directory.CreateDirectory(fDir + "\\" + tempFolder);
                if (account.JpegQuality == ThumbMaker.DEFAULTJPEGQUALITY) {
                    thumbMaker.ResizeToJpeg(account.ImageWidth, account.ImageHeight, bigName);
                    thumbMaker.ResizeToJpeg(account.ThumbWidth, account.ThumbHeight, thumbName);
                }
                else {
                    thumbMaker.ResizeToJpeg(account.ImageWidth, account.ImageHeight, long.Parse(account.JpegQuality.ToString()), bigName);
                    thumbMaker.ResizeToJpeg(account.ThumbWidth, account.ThumbHeight, long.Parse(account.JpegQuality.ToString()), thumbName);
                }

                ftpClient.Upload(bigName, account.ImageFolder);
                fileNumber++;
                File.Delete(bigName);
                ftpClient.Upload(thumbName, account.ThumbFolder);
                File.Delete(thumbName);
                Directory.Delete(fDir + "\\" + tempFolder, true);

                uploadedImagesCount++;
                progress.Status = (2 + (100 * uploadedImagesCount / imageNames.Count));
                progress.RefreshStatus();
            }

            progress.Close();
            MessageBox.Show(uploadedImagesCount + " image(s) uploaded...");
        }

        private void cbFTP_SelectedIndexChanged(object sender, System.EventArgs e) {
            if (cbFTP.SelectedItem != null) {
                Account account = (Account)cbFTP.SelectedItem;
                lblInfo.Text = account.Url + "\nbig: " + account.ImageWidth + " x " + account.ImageHeight + "\n thumb: " + account.ThumbWidth + " x " + account.ThumbHeight;
                SelectedAccount = account;
            }
        }

        private void btnNewAccount_Click(object sender, System.EventArgs e) {
            FTPSettings ftpSettingsDialog = new FTPSettings();
            if (ftpSettingsDialog.ShowDialog() == DialogResult.OK) {

            }
            RefreshFtpCombo();
        }

        private void btnRemoveFTP_Click(object sender, System.EventArgs e) {
            Account account = (Account)cbFTP.SelectedItem;
            FormGM.DataContext.Accounts.DeleteOnSubmit(account);
            FormGM.DataContext.SubmitChanges();
            RefreshFtpCombo();
        }

        private void btnTestAccount_Click(object sender, System.EventArgs e) {
            Account account = (Account)cbFTP.SelectedItem;
            FtpClient ftpClient = new FtpClient(account.Url, account.Username, CryptorEngine.Decrypt(account.Password, true, account.Username));
            // ftpClient.VerboseDebugging = true;
            try {
                // create test fail
                string testFailName = "test.txt";
                FileStream fileStream = File.Create(testFailName);
                fileStream.Flush();
                fileStream.Close();

                // Test Big Image folder existanse and upload permission
                ftpClient.Upload(testFailName, account.ImageFolder);
                ftpClient.Delete(FtpClient.NormalizeUrl(account.ImageFolder) + testFailName);

                // Test Thumb folder existanse and upload permission
                ftpClient.Upload(testFailName, account.ThumbFolder);
                ftpClient.Delete(FtpClient.NormalizeUrl(account.ThumbFolder) + testFailName);
                File.Delete(testFailName);

                MessageBox.Show("Account succesfuly tested!");
            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message + "\n" + ex.ToString());
            }
        }

        private void btnRotate270_Click(object sender, EventArgs e) {
            if (lvImages.SelectedItems.Count > 0) {
                List<string> selectedImages = new List<string>();
                foreach (ListViewItem selectedItem in lvImages.SelectedItems) {
                    selectedImages.Add((string)imageNames[selectedItem.Index]);
                }

                foreach (ListViewItem selectedItem in lvImages.SelectedItems) {
                    string imageName = (string)imageNames[selectedItem.Index];
                    Image image = Image.FromFile(imageName);
                    Bitmap bitmap = RotateImage.Utilities.RotateImage(image, 270F);
                    selectedItem.ImageList.Images.Clear();
                    image.Dispose();
                    ThumbMaker.SaveImage(bitmap, imageName, 100, ImageFormat.Jpeg);
                    bitmap.Dispose();
                }
                ReloadImages();

                SelectImages(selectedImages);
            }
        }

        private void btnRotate90_Click(object sender, EventArgs e) {
            if (lvImages.SelectedItems.Count > 0) {
                List<string> selectedImages = new List<string>();
                foreach (ListViewItem selectedItem in lvImages.SelectedItems) {
                    selectedImages.Add((string)imageNames[selectedItem.Index]);
                }

                foreach (ListViewItem selectedItem in lvImages.SelectedItems) {
                    string imageName = (string)imageNames[selectedItem.Index];
                    Image image = Image.FromFile(imageName);
                    Bitmap bitmap = RotateImage.Utilities.RotateImage(image, 90F);
                    selectedItem.ImageList.Images.Clear();
                    image.Dispose();
                    ThumbMaker.SaveImage(bitmap, imageName, 100, ImageFormat.Jpeg);
                    bitmap.Dispose();
                }
                ReloadImages();

                SelectImages(selectedImages);
            }
        }

        private void btnMoveLeft_Click(object sender, EventArgs e) {
            if (lvImages.SelectedItems.Count > 0) {
                List<string> selectedImages = new List<string>();
                foreach (ListViewItem selectedItem in lvImages.SelectedItems) {
                    selectedImages.Add((string)imageNames[selectedItem.Index]);
                }

                List<string> buffer = new List<string>();
                List<string> result = new List<string>();
                for (int i = (imageNames.Count - 1); i > -1; i--) {
                    string imageName = imageNames[i];
                    if (selectedImages.IndexOf(imageName) > -1) {
                        buffer.Add(imageName);
                    }
                    else {
                        result.Add(imageName);
                        if (buffer.Count > 0) {
                            result.AddRange(buffer);
                            buffer.Clear();
                        }
                    }
                }

                if (buffer.Count > 0) {
                    result.AddRange(buffer);
                    buffer.Clear();
                }

                imageNames = ReverseList(result);
                ReloadImages();
                SelectImages(selectedImages);
            }
        }

        private void btnMoveRight_Click(object sender, EventArgs e) {
            if (lvImages.SelectedItems.Count > 0) {
                List<string> selectedImages = new List<string>();

                foreach (ListViewItem selectedItem in lvImages.SelectedItems) {
                    selectedImages.Add((string)imageNames[selectedItem.Index]);
                }

                List<string> buffer = new List<string>();
                List<string> result = new List<string>();
                for (int i = 0; i < imageNames.Count; i++) {
                    string imageName = imageNames[i];
                    if (selectedImages.IndexOf(imageName) > -1) {
                        buffer.Add(imageName);
                    }
                    else {
                        result.Add(imageName);
                        if (buffer.Count > 0) {
                            result.AddRange(buffer);
                            buffer.Clear();
                        }
                    }
                }

                if (buffer.Count > 0) {
                    result.AddRange(buffer);
                    buffer.Clear();
                }

                imageNames = result;
                ReloadImages();
                SelectImages(selectedImages);
            }
        }

        #endregion

        #region Methods

        private void ReloadImages() {
            lvImages.Items.Clear();
            foreach (string imageName in imageNames) {
                FileInfo fi = new FileInfo(imageName);
                Image image1 = Image.FromFile(imageName);
                Image image2 = ImageResize.FixedSize(image1, 100, 100);
                lvImages.LargeImageList.Images.Add(image2);
                image1.Dispose();
                image2.Dispose();
                lvImages.Items.Add(new ListViewItem(fi.Name, (lvImages.LargeImageList.Images.Count - 1)));
            }
        }

        private void RefreshFtpCombo() {
            accounts = FTPSettings.GetAccounts();
            cbFTP.DisplayMember = "Name";
            cbFTP.DataSource = accounts;
            cbFTP.Update();
            cbFTP.Enabled = btnUpload.Enabled = btnFtpSettings.Enabled = btnTestAccount.Enabled = btnRemoveFTP.Enabled = (accounts.Count > 0);

        }

        private List<string> ReverseList(List<string> inc) {
            List<string> result = new List<string>();
            for (int i = (inc.Count - 1); i > -1; i--) {
                result.Add(inc[i]);
            }
            return result;
        }

        private void SelectImages(List<string> images) {
            foreach (string image in images) {
                foreach (ListViewItem item in lvImages.Items) {
                    string imageName = (string)imageNames[item.Index];
                    if (image == imageName) item.Selected = true;
                }
            }
        }

        #endregion

    }
}
